/*
 * $Id$
 */
package corejs.rt;

import static corejs.rt.Invocable.*;

/**
 *
 * @author pengyu
 */
//http://repl.it/languages/JavaScript
public class JsObjects {
   
   public static final Value createGlobalObject() {
      return new Obj(null) {{
         store("print", new Print());
         
         store("Array", Array._constructor_);
         store("String", Str._constructor_);
         //store("Function", _Function);
         store("NaN", Number.NaN);
         store("undefined", Undefined.UNDEFINED);

         // isNaN
         store("isNaN", new F1() {
            @Override
            public Bool invoke(final ExecutionContext ec) {
               return ec.get(_arg_).isNaN() ? Bool.TRUE : Bool.FALSE;
            }
         });

         // assertEquals
         store("assertEquals", new F2() {
            @Override
            public Bool invoke(final ExecutionContext ec) {
               final Value expected = ec.get(_arg1_);
               final Value actual = ec.get(_arg2_);
               if (!expected.equals(actual, ec)) {
                  throw new AssertionError("expected <" + expected 
                          + ">, but was: " + actual);
               }
               return Bool.TRUE;
            }
         }); 
         
         // assertTrue
         store("assertTrue", new F1() {
            @Override
            public Bool invoke(final ExecutionContext ec) {
               if (!ec.get(_arg_).toBool().isTrue()) {
                  throw new AssertionError("expected <true>, but was: false");
               }
               return Bool.TRUE;
            }
         });

         // assertFalse
         store("assertFalse", new F1() {
            @Override
            public Bool invoke(final ExecutionContext ec) {
               if (ec.get(_arg_).toBool().isTrue()) {
                  throw new AssertionError("expected <false>, but was: true");
               }
               return Bool.TRUE;
            }
         });

         // assertUndefined
         store("assertUndefined", new F1() {
            @Override
            public Value invoke(final ExecutionContext ec) {
               final Value val = ec.get(_arg_);
               if (val.isUndefined()) {
                  return Bool.TRUE;
               }
               throw new AssertionError("expected <undefined>, but was: "
                     + Value.toPrimitive(val, ec).toStr());
            }
         });

         store("Date", Date.newInstance());
         store("Object", Object_.newInstance());
      }}; // _END_OF: return new JsObject(null) {{
   }   
   
   static {   
      Obj._obj_init_prototype_();
      Str._str_init_prototype_();
      Invocable._fn_init_prototype_();
      Array._array_init_prototype_();
      Number._number_init_prototype_();
   }

}
